import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    private String id;
    private String name;
    private String dormitoryBuilding;
    private String roomNumber;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDormitoryBuilding() {
        return dormitoryBuilding;
    }

    public void setDormitoryBuilding(String dormitoryBuilding) {
        this.dormitoryBuilding = dormitoryBuilding;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }
}

class Dormitory {
    private String building;
    private String roomNumber;
    private List<Student> students;

    public Dormitory(String building, String roomNumber) {
        this.building = building;
        this.roomNumber = roomNumber;
        this.students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        this.students.add(student);
    }

    // Getters and Setters
    public String getBuilding() {
        return building;
    }

    public void setBuilding(String building) {
        this.building = building;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }
}

class ExcelReader {
    public static List<Student> readStudentsFromExcel(String filePath) throws IOException {
        List<Student> students = new ArrayList<>();
        FileInputStream fis = new FileInputStream(filePath);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

        for (Row row : sheet) {
            if (row.getRowNum() == 0) continue; // 跳过标题行
            Student student = new Student(
                row.getCell(0).getStringCellValue(),
                row.getCell(1).getStringCellValue()
            );
            students.add(student);
        }

        workbook.close();
        fis.close();
        return students;
    }
}

class ExcelWriter {
    public static void writeStudentsToExcel(List<Student> students, String filePath) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("学生信息");

        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("ID");
        headerRow.createCell(1).setCellValue("姓名");

        int rowNum = 1;
        for (Student student : students) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(student.getId());
            row.createCell(1).setCellValue(student.getName());
        }

        FileOutputStream fos = new FileOutputStream(filePath);
        workbook.write(fos);
        workbook.close();
        fos.close();
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Dormitory> dormitories = new ArrayList<>();
        List<Student> students = new ArrayList<>();

        while (true) {
            System.out.println("请输入操作编号：\n1. 添加宿舍\n2. 添加学生\n3. 查询学生住宿情况\n4. 导出学生住宿信息\n5. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addDormitory(scanner, dormitories);
                    break;
                case 2:
                    addStudent(scanner, students, dormitories);
                    break;
                case 3:
                    queryStudentAccommodation(scanner, students);
                    break;
                case 4:
                    exportStudentAccommodation(students);
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选项！");
            }
        }
    }

    private static void addDormitory(Scanner scanner, List<Dormitory> dormitories) {
        System.out.print("请输入宿舍楼名称：");
        String building = scanner.nextLine();
        System.out.print("请输入房间号：");
        String roomNumber = scanner.nextLine();
        Dormitory dormitory = new Dormitory(building, roomNumber);
        dormitories.add(dormitory);
        System.out.println("宿舍添加成功！");
    }

    private static void addStudent(Scanner scanner, List<Student> students, List<Dormitory> dormitories) {
        System.out.print("请输入学生ID:");
        String id = scanner.nextLine();
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();

        Student student = new Student(id, name);
        students.add(student);

        System.out.print("请选择宿舍楼名称：");
        String building = scanner.nextLine();
        System.out.print("请选择房间号：");
        String roomNumber = scanner.nextLine();

        for (Dormitory dormitory : dormitories) {
            if (dormitory.getBuilding().equals(building) && dormitory.getRoomNumber().equals(roomNumber)) {
                dormitory.addStudent(student);
                System.out.println("学生入住成功！");
                return;
            }
        }
        System.out.println("找不到该宿舍，请检查输入！");
    }

    private static void queryStudentAccommodation(Scanner scanner, List<Student> students) {
        System.out.print("请输入学生ID:");
        String id = scanner.nextLine();

        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("学生住宿信息：" + student.getName() + " - " + student.getDormitoryBuilding() + " " + student.getRoomNumber());
                return;
            }
        }
        System.out.println("未找到该学生！");
    }

    private static void exportStudentAccommodation(List<Student> students) {
        try {
            ExcelWriter.writeStudentsToExcel(students, "D:\\Java\\学生宿舍管理·系统\\demo\\students.xlsx");
            System.out.println("学生住宿信息导出成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}